package com.dynamicthreadpool.strategy;

import com.dynamicthreadpool.spi.ExtensionLoader;
import com.dynamicthreadpool.spi.URL;

/**
 * 拒绝策略工厂 - 基于SPI机制
 */
public final class RejectStrategyFactory {

    private static final ExtensionLoader<RejectStrategy> EXTENSION_LOADER = 
            ExtensionLoader.getExtensionLoader(RejectStrategy.class);

    /**
     * 获取拒绝策略
     *
     * @param name 策略名称
     * @return 拒绝策略实例
     */
    public static RejectStrategy getStrategy(String name) {
        if (name == null || name.isEmpty()) {
            return EXTENSION_LOADER.getDefaultExtension();
        }
        return EXTENSION_LOADER.getExtension(name);
    }
    
    /**
     * 根据URL获取拒绝策略
     *
     * @param url URL对象，包含线程池配置信息
     * @return 拒绝策略实例
     */
    public static RejectStrategy getStrategy(URL url) {
        String strategyName = url.getParameter("reject", "abort");
        return getStrategy(strategyName);
    }
}